package com.atguigu.gmall.realtime.app;

import com.atguigu.gmall.realtime.common.Constant;
import com.atguigu.gmall.realtime.util.SQLUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import static org.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION;

/**
 * @Author lzc
 * @Date 2022/12/2 13:56
 */
public abstract class BaseSQLApp {
    protected abstract void handle(StreamExecutionEnvironment env,
                                   StreamTableEnvironment tEnv);
    
    public void init(int port, int p, String ckAndAndJobName) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        // 任务: 从 ods_db 读取数据, 过滤出需要的维度表的数据, 不同的维度表数据写出到 Phoenix 的不同表中
        
        // 1. 读取ods_db 数据
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        //conf.setString("pipeline.name", ckAndAndJobName);  // 在flink 中, 所有都可以使用这个参数设置 jobName
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        
        env.setParallelism(p);  // 并行度的设置原则: 和 kafka 的 topic 的分区数保持一致
        
        // 开启 checkpoint, 严格一次
        // 1. 设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        // 2. 启用 checkpoint 并设置 checkpoint 的周期
        env.enableCheckpointing(3000);  // 公司中: 一般是分钟界别
        // 3. 设置 checkpoint 的语义
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        // 4. checkpoint 的存储目录
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/gmall/" + ckAndAndJobName);
        // 5. 超时时间
        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000);
        // 6. 设置 checkpoint 的并发数
        //env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        // 7. checkpoint 的之间的间隔:   6 和 7 一般只设置一个
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        // 8. 当 job 取消的时候的,checkpoint 的保留策略
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(RETAIN_ON_CANCELLATION); // 1.13.6 使用这个
    
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        // 给 sql 应用设置名字
        tEnv.getConfig().getConfiguration().setString("pipeline.name", ckAndAndJobName);
        
        handle(env, tEnv);
    
    }
    
    public void readOdsDb(StreamTableEnvironment tEnv, String groupId){
        tEnv.executeSql("create table ods_db(" +
                            " `database` string, " +
                            " `table` string, " +
                            " `type` string, " +
                            " `data` map<string, string>, " +
                            " `old` map<string, string>, " +
                            " `ts` bigint," +
                            " `pt` as proctime()," +
                            " `et` as to_timestamp_ltz(ts, 0)," +
                            "  watermark for `et` as `et` - interval '3' second " +
                            ")" + SQLUtil.getKafkaSourceDDL(Constant.TOPIC_ODS_DB, groupId, "json"));
    }
    
    public void readBaseDic(StreamTableEnvironment tEnv) {
        tEnv.executeSql("create table base_dic (" +
                            "  dic_code string," +
                            "  dic_name string " +
                            ") WITH (" +
                            "  'connector' = 'jdbc'," +
                            "  'url' = 'jdbc:mysql://hadoop162:3306/gmall2022?useSSL=false'," +
                            "  'table-name' = 'base_dic', " +
                            "  'username' = 'root', " +
                            "  'lookup.cache.ttl' = '30 s', " +  // 用来设置维度表的数据的ttl 时间
                            "  'lookup.cache.max-rows' = '10', " +  // 缓存的最大行数
                            "  'password' = 'aaaaaa' " +
                            ")");
    }
    
}
